package org.raymond.iworks.study.netty.official.fundamental.discard;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * @author raymond
 * @version V1.0
 * @Description: Handles a server-side channel.
 */
public class DiscardServerHandler0 extends ChannelInboundHandlerAdapter { // (1)
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // (2)
        // discard
        // ((ByteBuf) msg).release(); // (3)
        try {
            // Do something with msg
        } finally {
            ReferenceCountUtil.release(msg); // 应用程序的责任,释放消息
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
    // 当有I/O错误或其它异常信息从Netty中抛出时exceptionCaught()方法将会被调用.大多数情况下,捕捉到异常后应该将其记录到日志中并且关闭channel,
    // 当然该方法的实现会有所不同,主要还是根据你自己的情况来处理异常.例如你想要在连接关闭前发送一个带有错误码的回应消息.
}
